using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHealth : MonoBehaviour
{
    public int health;
    public int Blinks;
    public float time;
    public float dieTime;
    private Animator anim;

    private Renderer myRenderer;
    // Start is called before the first frame update
    void Start()
    {
        myRenderer = GetComponent<Renderer>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void DamegePlayer(int damage) {
        health -= damage;
        if (health <= 0) {
            anim.SetTrigger("Die");
        }
        BlinkPlayer(Blinks, time);
    }

    void KillPlayer() {
        Invoke("DestroyPlayer", dieTime);
    }

    void DestroyPlayer() {
        Destroy(gameObject);
    }

    void BlinkPlayer(int numBlinks, float seconds) {
        StartCoroutine(DoBlinks(numBlinks, seconds));
    }

    IEnumerator DoBlinks(int numBlinks, float seconds) {
        for (int i = 0; i < numBlinks * 2; i++) {
            //BlinkPlayer(1, seconds);
            myRenderer.enabled = !myRenderer.enabled;
            yield return new WaitForSeconds(seconds);
        }
        myRenderer.enabled = true;
    }
}
